package cn.redragon.soa.model.mapper;


import cn.redragon.soa.common.util.JsonUtil;
import cn.redragon.soa.data.entity.SystemLogEntity;
import cn.redragon.soa.model.dto.SystemLogDTO;
import com.fasterxml.jackson.databind.JsonNode;
import org.mapstruct.Builder;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE, builder = @Builder(disableBuilder = true))
public interface SystemLogMapper {

    SystemLogMapper INSTANCE = Mappers.getMapper(SystemLogMapper.class);

    @Mapping(source = "requestHeader", target = "requestHeader", qualifiedByName = "stringToJsonNode")
    @Mapping(source = "requestPayload", target = "requestPayload", qualifiedByName = "stringToJsonNode")
    @Mapping(source = "response", target = "response", qualifiedByName = "stringToJsonNode")
    SystemLogEntity mapper(SystemLogDTO systemLogDTO);

    @Named("stringToJsonNode")
    default JsonNode stringToJsonNode(String value) {
        return JsonUtil.stringToJsonNode(value);
    }
}
